package com.zsummer.zero.tool.apache.io;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

public class FileDemo {

	public static void main(String[] args) {
		// run1();
		// run2();
		// run3();
		// run4();
		// run5();
		// run6();
		// run7();
		// run8();
		// run9();
		run10();
	}

	// 递归删除
	private static void run1() {
		File file = FileUtils.getFile("C:\\Users\\zsummer\\Desktop\\a");
		System.out.println(file.exists());

		try {
			FileUtils.deleteDirectory(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// FileUtis的get方法
	private static void run2() {
		String userPath = FileUtils.getUserDirectoryPath();
		System.out.println(userPath);
		File file = FileUtils.getFile("./");
		File file1 = FileUtils.getFile("");
		System.out.println(file.getAbsolutePath());
		System.out.println(file.isDirectory());
		System.out.println(file1.isDirectory());
		String tempDirectoryPath = FileUtils.getTempDirectoryPath();
		System.out.println(tempDirectoryPath);
	}

	// 删除目录下的内容，但是不删除目录
	private static void run3() {
		try {
			FileUtils.cleanDirectory(FileUtils.getFile("C:\\Users\\zsummer\\Desktop\\a"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 比较两个文件是否相等
	private static void run4() {
		File file1 = FileUtils.getFile("C:\\Users\\zsummer\\Desktop\\a\\a.txt");
		File file2 = FileUtils.getFile("C:\\Users\\zsummer\\Desktop\\a\\b\\b.txt");
		try {
			System.out.println(file1.length());
			System.out.println(file2.length());
			boolean contentEquals = FileUtils.contentEquals(file1, file2);
			System.out.println(contentEquals);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 复制
	private static void run5() {
		File file1 = FileUtils.getFile("C:\\Users\\zsummer\\Desktop\\a\\a.txt");
		File file2 = FileUtils.getFile("C:\\Users\\zsummer\\Desktop\\a\\z.txt");
		try {
			FileUtils.copyFile(file1, file2);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 遍历 返回迭代器
	private static void run6() {
		File file1 = FileUtils.getFile("C:\\Users\\zsummer\\Desktop\\a");
		// Iterator<File> iterateFiles = FileUtils.iterateFiles(file1, new String[]
		// {"txt", "html"}, true);
		Iterator<File> iterateFiles = FileUtils.iterateFiles(file1, null, true);
		while (iterateFiles.hasNext()) {
			File file = iterateFiles.next();
			System.out.println(file.getName());
		}
	}

	// 文件行读取
	private static void run7() {
		File file1 = FileUtils.getFile("C:\\Users\\zsummer\\Desktop\\a\\a.txt");
		// LineIterator lineIterator;
		// try {
		// lineIterator = FileUtils.lineIterator(file1);
		// while (lineIterator.hasNext()) {
		// String line = lineIterator.next();
		// System.out.println(line);
		// }
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		try {
			List<String> readLines = FileUtils.readLines(file1, "UTF-8");
			for (String string : readLines) {
				System.out.println(string);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 文件读取
	private static void run8() {
		File file1 = FileUtils.getFile("C:\\Users\\zsummer\\Desktop\\a\\a.txt");
		try {
			String readFileToString = FileUtils.readFileToString(file1, "UTF-8");
			System.out.println(readFileToString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 文件大小，单位byte
	private static void run9() {
		File file1 = FileUtils.getFile("C:\\Users\\zsummer\\Desktop\\a\\a.txt");
		System.out.println(FileUtils.sizeOf(file1));
	}

	// 文件写入
	private static void run10() {
		String a= "abcdefdfjawoifwjio fjwi fjwao \n jfojiw fwjo \n";
		File file = FileUtils.getFile("C:\\Users\\zsummer\\Desktop\\a\\a.txt");
		try {
			FileUtils.write(file, a, "UTF-8", true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
